package cn.deepbit.service.thingsboard.controller;

import org.anyline.controller.impl.AnylineController;
import org.anyline.entity.DataRow;
import org.anyline.entity.DataSet;
import org.anyline.thingsboard.util.ThingsBoardClient;
import org.anyline.util.BasicUtil;
import org.anyline.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController("dp.gps.controller")
@RequestMapping("/dp/service/gps")
public class GPSController extends AnylineController {


    @Autowired(required = false)
    private ThingsBoardClient thingsBoardClient;


    @RequestMapping("test")
    public Object test() {
        String name = getParam("name");
        System.out.println("name="+name);
        return success();
    }
    @RequestMapping("trace")
    public Object trace(){
        if(null == thingsBoardClient){
            return fail("ThingsBoard服务异常");
        }
        long start = 0;
        long end = 0;
        String tb = getParam("tb");
        String fr = getParam("fr");
        String to = getParam("to");
        if(BasicUtil.isEmpty(tb)){
            return fail("未提供ThingsBoard设备ID(tb)");
        }
        if(BasicUtil.isEmpty(fr)){
            return fail("未提供开始时间(fr)(13位毫秒或yyyy-MM-dd HH:mm:ss)");
        }
        int limit = getInt("limit", 1000);   //第段时间最多查多少个点
        if(BasicUtil.isNumber(fr)){
            start = BasicUtil.parseLong(fr, System.currentTimeMillis());
        }else{
            start = DateUtil.parse(fr,"yyyy-MM-dd HH:mm:ss").getTime();
        }
        if(BasicUtil.isEmpty(to)){
            end = System.currentTimeMillis();
        }else if(BasicUtil.isNumber(to)){
            end = BasicUtil.parseLong(to, System.currentTimeMillis());
        }else{
            end = DateUtil.parse(to,"yyyy-MM-dd HH:mm:ss").getTime();
        }

        //未下线 并且 上线时间在 查询区间之前
        DataSet records = service.querys("IOT_DEVICE_STATUS(ID,ONLINE_TIME,OFFLINE_TIME)"
                        , "TB_ID:"+tb, "ONLINE_TIME <= " + start, "OFFLINE_TIME=0", "");

        //上线时间在查询区间之内
        records = records.union(service.querys("IOT_DEVICE_STATUS(ID,ONLINE_TIME,OFFLINE_TIME)"
                , "TB_ID:"+tb, "ONLINE_TIME >= " + start + " AND ONLINE_TIME <= " + end),
        "ID");

        //查询开始时间  在上下线区间内
        records = records.union(service.querys("IOT_DEVICE_STATUS(ID,ONLINE_TIME,OFFLINE_TIME)"
                        , "TB_ID:"+tb, "ONLINE_TIME <= " + start + " AND OFFLINE_TIME >= " + start)
                ,"ID"
        );
        for(DataRow record:records){
            long on = record.getLong("ONLINE_TIME",0);
            long off = record.getLong("OFFLINE_TIME",0);
            if(off ==0){
                off = System.currentTimeMillis();
            }
            DataSet traces = thingsBoardClient.getDeviceTimeseries(tb,"lng,lat", on, off, limit);
            record.put("traces", traces);
        }

        return records;
    }
}
